package com.meichis.yndms.adapter.RecycleViewItemType;


import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.ImageAdapter;
import com.meichis.yndms.model.InspectResult_Detail;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.model.UserInfo;

import java.util.ArrayList;

public class FillMode_PictureDP implements ItemViewDelegate<InspectResult_Detail> {
    private ArrayList<InspectResult_Detail> items;
    private String addr, staffCode;
    public FillMode_PictureDP(ArrayList<InspectResult_Detail> items) {
        addr = SharePreferenceUtil.getInstance().getStringValue(SharePreferenceUtil.PREFERENCES_VSTLOCATION);
        staffCode = ((UserInfo) SharePreferenceUtil.getInstance().getObjectValue(SharePreferenceUtil.PREFERENCES_USERINFO)).getUsername();
        this.items = items;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_picture_dp;
    }

    @Override
    public boolean isForViewType(InspectResult_Detail item, int position) {
        if (item.getFillMode() == 9)
            return true;
        return false;
    }

    @Override
    public void convert(final ViewHolder holder, final InspectResult_Detail item, int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        }

        holder.setText(R.id.tv_question, String.valueOf(Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getInspectItemName() +
                (!TextUtils.isEmpty(item.getUnit()) ? "(" + item.getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getSubTitle() + "</small></font>" : "")
        )));
        RecyclerView RV_photoList = holder.getView(R.id.RV_photoList);
        RV_photoList.setLayoutManager(new LinearLayoutManager(holder.getConvertView().getContext(), LinearLayoutManager.HORIZONTAL, false));
        ArrayList<Picture> pictures = new ArrayList<>();
        Picture p;
        for (String s : item.getFillName().split("\\|")) {
            p = new Picture();
            p.setGuid(s);
            pictures.add(p);
        }
        ImageAdapter imageAdapter = new ImageAdapter(holder.getConvertView().getContext(), R.layout.item_image , pictures);
        RV_photoList.setAdapter(imageAdapter);
    }

}
